package org.ishequ.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ishequ.domain.News;
import org.ishequ.service.WebService;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Android_WebActivity extends Activity {
	
	private EditText address;
	private ImageView imageView;
	private TextView htmlText;
	private ScrollView scrollView;
	private ListView listView;
	private WebService service;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        service = new WebService();
        address = (EditText) findViewById(R.id.address);
        imageView = (ImageView) findViewById(R.id.pic);
        htmlText = (TextView) findViewById(R.id.html);
        scrollView = (ScrollView) findViewById(R.id.scroll);
        listView = (ListView) findViewById(R.id.listview);
        
        scrollView.setVisibility(View.GONE);
        imageView.setVisibility(View.GONE);
        listView.setVisibility(View.GONE);
    }
    
    public void showImg(View v){
    	address.setText(R.string.path1);
    	if(!"".equals(address.getText().toString().trim())){
    		imageView.setVisibility(View.VISIBLE);
    		scrollView.setVisibility(View.GONE);
    		listView.setVisibility(View.GONE);
    		try {
				byte[] data = service.getImg(address.getText().toString());
				Bitmap img = BitmapFactory.decodeByteArray(data, 0, data.length);
				imageView.setImageBitmap(img);
			} catch (Exception e) {
				e.printStackTrace();
			}
    	}
    }
    
    public void showHtml(View v){
    	address.setText(R.string.path2);
    	if(!"".equals(address.getText().toString().trim())){
    		scrollView.setVisibility(View.VISIBLE);
    		imageView.setVisibility(View.GONE);
    		listView.setVisibility(View.GONE);
    		try {
				String html = service.getHtml(address.getText().toString());
				htmlText.setText(html);
				Toast.makeText(getApplicationContext(), html, 0);
			} catch (Exception e) {
				e.printStackTrace();
			}
    	}
    }
    
    public void showXml(View v){
    	address.setText(R.string.path3);
    	if(!"".equals(address.getText().toString().trim())){
    		scrollView.setVisibility(View.GONE);
    		imageView.setVisibility(View.GONE);
    		listView.setVisibility(View.VISIBLE);
    		fillListView(listView,"xml");
    	}	
    }
    
    public void showJson(View v){
    	address.setText(R.string.path4);
    	if(!"".equals(address.getText().toString().trim())){
    		scrollView.setVisibility(View.GONE);
    		imageView.setVisibility(View.GONE);
    		listView.setVisibility(View.VISIBLE);
    		fillListView(listView,"json");
    	}
    }
    
    public void fillListView(ListView listView,String type){
    	List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
    	List<News> newslist = null;
		try {
			if("json".equals(type))
				newslist = service.getJsonNews(address.getText().toString().trim());
			else
				newslist = service.getXmlNews(address.getText().toString().trim());
		} catch (Exception e) {
			e.printStackTrace();
		}
    	for(News news :newslist){
    		Map<String,Object> map = new HashMap<String,Object>();
    		map.put("id", news.getId());
    		map.put("name", news.getName());
    		map.put("timelen", news.getTimelen());
    		data.add(map);
    	}
    	SimpleAdapter adapter = 
    		new SimpleAdapter(getApplicationContext(), data, 
    				R.layout.item, new String[]{"id","name","timelen"}, 
    				new int[]{R.id.id,R.id.name,R.id.time});
    	listView.setAdapter(adapter);
    }
    
}